﻿using AntdUI;
using WorkTool2.Classes.Data;
using WorkTool2.Enums;
using WorkTool2.Helpers;

namespace WorkTool2.Controls
{
    public partial class Calendar : UserControl
    {
        private Form form;

        public Calendar(Form _form)
        {
            form = _form;
            InitializeComponent();
            calendar1.ShowChinese = true;
            calendar1.Full = true;
            calendar1.ShowButtonToDay = true;
            calendar1.Invalidate();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            ITask.Run(() =>
            {
                var calendarDataList = new List<CalendarData>();
                foreach (var year in SettingsHelper._calendarSettings.Select(x => x.Year).Distinct())
                {
                    var temp = CalendarHelper.GetCalendarSettings(year);
                    calendarDataList.AddRange(temp);
                }
                calendar1.BadgeAction = dates =>
                {
                    var result = new List<DateBadge>();

                    for (DateTime day = dates[0].Date; day <= dates[1].Date; day = day.AddDays(1))
                    {
                        var dayData = calendarDataList.Where(x => x.Date.Equals(day)).OrderBy(x => x.CalendarDateStatus).FirstOrDefault();
                        if (dayData != null)
                        {
                            Color color = Color.YellowGreen;
                            var description = "";
                            switch (dayData.CalendarDateStatus)
                            {
                                case CalendarDateStatus.WorkingDays:
                                    break;

                                case CalendarDateStatus.WorkingDays1:
                                    color = Color.OrangeRed;
                                    description = dayData.Description;
                                    break;

                                case CalendarDateStatus.DayOff:
                                    description = dayData.Description;
                                    break;

                                case CalendarDateStatus.Custom:
                                    description = dayData.Description;
                                    color = Color.DarkBlue;
                                    break;

                                case CalendarDateStatus.Leave:
                                    color = Color.Orange;
                                    description = dayData.CalendarLeaveData.LeaveName + " " + (dayData.CalendarLeaveData.LeaveTimeType.Equals(LeaveTimeType.All) ? "" : dayData.CalendarLeaveData.LeaveTimeType.ToString());
                                    break;

                                default:
                                    break;
                            }
                            result.Add(new DateBadge(day.ToString("yyyy-MM-dd"), description, color));
                        }
                        else
                        {
                            if ((int)day.DayOfWeek == 0 || (int)day.DayOfWeek == 6)
                            {
                                result.Add(new DateBadge(day.ToString("yyyy-MM-dd"), "休", Color.YellowGreen));
                            }
                        }
                    }
                    return result;
                };
                calendar1.LoadBadge();
            });
        }

        private void Calendar_DateChanged(object sender, AntdUI.DateTimeEventArgs e)
        {
            //Invalidate();
            //calendar1.LoadBadge();

            //AntdUI.Message.info(form, e.Value.ToString("yyyy-MM-dd"), autoClose: 1);
        }
    }
}